package com.xiaoma.myaudience.biz.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.xiaoma.myaudience.R;

public class LoadingListView extends FrameLayout {
	private int mLoadingLayoutID = -1;
	private int mLoadingFailedLayoutID = -1;
	private View mLoadingLayout;
	private View mLoadingFailedLayout;
	private ListView mListView;
	private int mLoadingMoreLayoutID = -1;
	private View mLoadingMoreLayout;
	private Button mListMoreBtn;
	private ProgressBar mListMorePb;
	private int mEmptyViewID = -1;
	private View mEmptyView;
	private int mCacheColorHint;

	public LoadingListView(Context context) {
		this(context, null);
	}

	public LoadingListView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public LoadingListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.LoadingListView, defStyle, 0);
		mLoadingLayoutID = a.getResourceId(
				R.styleable.LoadingListView_loading_layout, -1);
		mLoadingFailedLayoutID = a.getResourceId(
				R.styleable.LoadingListView_loading_failed_layout, -1);
		mLoadingMoreLayoutID  = a.getResourceId(
				R.styleable.LoadingListView_loading_more_layout, -1);
		mEmptyViewID = a.getResourceId(
                R.styleable.LoadingListView_empty_view, -1);
		mCacheColorHint = a.getColor(
                R.styleable.LoadingListView_cacheColorHint, 0);
		a.recycle();
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		
		LayoutInflater inflater = LayoutInflater.from(getContext());
		
		mListView = new ListView(getContext());
		if (mLoadingMoreLayoutID != -1) {
			mLoadingMoreLayout = inflater.inflate(
					mLoadingMoreLayoutID, null);
		} else {
			initDefaultLoadingMoreLayout();
		}
		//mLoadingMoreLayout.setVisibility(View.GONE);
		mListView.addFooterView(mLoadingMoreLayout, null, false);
		addView(mListView, new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.FILL_PARENT,
				FrameLayout.LayoutParams.FILL_PARENT));
		
		mListView.setCacheColorHint(mCacheColorHint);
		mListView.setFadingEdgeLength(0);
		mListView.setSelector(R.drawable.list_view_selector);

		if (mEmptyViewID != -1) {
		    mEmptyView = inflater.inflate(mEmptyViewID, null);
		    addView(mEmptyView, new FrameLayout.LayoutParams(
	                FrameLayout.LayoutParams.FILL_PARENT,
	                FrameLayout.LayoutParams.FILL_PARENT));
		    mListView.setEmptyView(mEmptyView);
		}
		
		if (mLoadingLayoutID != -1) {
			mLoadingLayout = inflater.inflate(
					mLoadingLayoutID, null);
			addView(mLoadingLayout, new FrameLayout.LayoutParams(
					FrameLayout.LayoutParams.FILL_PARENT,
					FrameLayout.LayoutParams.FILL_PARENT));
		}
		if (mLoadingFailedLayoutID != -1) {
			mLoadingFailedLayout = inflater.inflate(
					mLoadingFailedLayoutID, null);
			addView(mLoadingFailedLayout, new FrameLayout.LayoutParams(
					FrameLayout.LayoutParams.FILL_PARENT,
					FrameLayout.LayoutParams.FILL_PARENT));
		}
	}
	
	private void initDefaultLoadingMoreLayout() {
		mLoadingMoreLayout = LayoutInflater.from(getContext()).inflate(R.layout.more_layout, null);
		mListMoreBtn = (Button)mLoadingMoreLayout.findViewById(R.id.list_more_btn);
		mListMorePb = (ProgressBar)mLoadingMoreLayout.findViewById(R.id.list_more_pb);
		mListMorePb.setVisibility(View.GONE);

	}
	
	public boolean onClickDefaultMoreLayout() {
		if(mListMorePb.getVisibility() == View.VISIBLE) return false;
		mLoadingMoreLayout.setVisibility(View.VISIBLE);
		mListMorePb.setVisibility(View.VISIBLE);
		mListMoreBtn.setText(getContext().getResources().getString(R.string.loading_text));
		return true;
	}
	
	public void finishClickDefaultMoreLayout() {
		mLoadingMoreLayout.setVisibility(View.VISIBLE);
		mListMorePb.setVisibility(View.GONE);
		mListMoreBtn.setText(getContext().getResources().getString(R.string.show_more));
	}
	
	public void showLoadingMoreLayout() {
		mLoadingMoreLayout.setVisibility(View.VISIBLE);
	}
	
	public void hideLoadingMoreLayout() {
		mLoadingMoreLayout.setVisibility(View.GONE);
	}

	public ListView getListView() {
		return mListView;
	}
	
	public View getLoadingMoreLayout() {
		return mLoadingMoreLayout;
	}
	
	public Button getLoadingMoreButton() {
		return mListMoreBtn;
	}

	public void showListView() {
		mListView.setVisibility(View.VISIBLE);
		if (mLoadingLayout != null)
			mLoadingLayout.setVisibility(View.GONE);
		if (mLoadingFailedLayout != null)
			mLoadingFailedLayout.setVisibility(View.GONE);
	}

	public void showLoading() {
		mListView.setVisibility(View.GONE);
		if (mLoadingLayout != null)
			mLoadingLayout.setVisibility(View.VISIBLE);
		if (mLoadingFailedLayout != null)
			mLoadingFailedLayout.setVisibility(View.GONE);
	}

	public void showLoadingFailed() {
		mListView.setVisibility(View.GONE);
		if (mLoadingLayout != null)
			mLoadingLayout.setVisibility(View.GONE);
		if (mLoadingFailedLayout != null)
			mLoadingFailedLayout.setVisibility(View.VISIBLE);
	}

	public int getListViewVisibility() {
		if (mListView != null)
			return mListView.getVisibility();
		return View.GONE;
	}

	public int getLoadingVisibility() {
		if (mLoadingLayout != null)
			return mLoadingLayout.getVisibility();
		return View.GONE;
	}

	public int getLoadingFailedVisibility() {
		if (mLoadingFailedLayout != null)
			return mLoadingFailedLayout.getVisibility();
		return View.GONE;
	}
	
	public View getEmptyView() {
	    return mEmptyView;
	}
}
